<?php

class Controller_Admin_Lesson extends Controller_Admin_Base
{
	// 列表
    public function action_index()
    {
    	$id = (int)$this->request->param('id');
    	
    	$count = DB::select(DB::expr("COUNT(*) AS total"))
                ->from('lesson')
                ->where('chapter_id', '=', $id)
                ->execute()
                ->get('total');
        $pagination = Pagination::factory(array(
            'total_items' => $count
        ));

        $records = DB::select('l.*', array('c.name', 'chapter_name'))
        		->from(array('lesson', 'l'))
        		->join(array('chapter', 'c'), 'LEFT')
        		->on('l.chapter_id', '=', 'c.id')
                ->where('chapter_id', '=', $id)
                ->order_by('c.order', 'ASC')
                ->limit($pagination->items_per_page)
                ->offset($pagination->offset)
                ->execute()->as_array();

        $this->prepare('课时列表');
        $this->content->records = $records;
        $this->content->pagination = $pagination->render();
        $this->render();
    }
    
    public function action_add()
    {
    	if ($_POST) {
    		$lesson = ORM::factory('lesson');
    		$lesson->values($_POST);
    		
    		$validate = $lesson->validate();
    		if (!$lesson->check()) {
    			$this->message($validate->errors('lesson'));
    			return;
    		}
    		
    		if (!$lesson->save()) {
                $this->message('操作失败');
                return;
            }
            $this->message('添加成功', "lesson/index/{$_POST['chapter_id']}");
    	} else {
    		$this->prepare('新增课时');
    		$this->content->chapters = $this->getChapterList();
        	$this->render();
    	}
    }
    
    public function action_edit()
    {
    	if ($_POST) {
    		$id = Arr::get($_POST, 'id');
    		
    		unset($_POST['id']);
    		
    		$lesson = ORM::factory('lesson');
    		$lesson->find($id);
            if ( ! $lesson->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }
            
            $old_chapter = $lesson->chapter_id;
    		
    		$lesson->values($_POST);
    		
    		$validate = $lesson->validate();
    		if (!$lesson->check()) {
    			$this->message($validate->errors('lesson'));
    			return;
    		}
    		
    		if (!$lesson->save()) {
                $this->message('操作失败');
                return;
            }
            $this->message('修改成功', "lesson/index/{$old_chapter}");
    	} else {
    		$id = (int)$this->request->param('id');

            if (empty($id)) {
                $this->message('参数错误！');
                return;
            }

            $lesson = ORM::factory('lesson')->find($id);

            if ( ! $lesson->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }
    		$this->prepare('编辑课时');
    		$this->content->record = $lesson->as_array();
    		$this->content->chapters = $this->getChapterList();
        	$this->render();
    	}
    }
    
	// 删除
    public function action_delete()
    {
        $id = (int) $this->request->param('id');

        if (empty($id)) {
            $this->message('参数错误！');
            return;
        }

        $lesson = ORM::factory('lesson')->find($id);

        if ( ! $lesson->loaded()) {
            $this->message('指定的记录不存在！');
            return;
        }
        
        $chapter_id = $lesson->chapter_id;

        if ($lesson->delete()) {
            $this->message('删除成功！', "lesson/index/{$chapter_id}");
        } else {
            $this->message('删除失败');
        }
    }
    
    //==========================================================================
    // Utils
    //==========================================================================
    
    /**
     * 获取章节列表
     */
 	private function getChapterList()
    {
    	$chapters = array();
    	foreach (Model_Chapter::getList() as $chapter) {
    		$chapters[$chapter['id']] = $chapter['name'];
    	}
    	
    	return $chapters;
    }
}